package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "套餐管理相关接口")
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetmealController {

    private final SetmealService setmealService;
    public SetmealController(SetmealService setmealService) {
        this.setmealService = setmealService;
    }

    /**
     * 套餐管理分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "套餐管理分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐管理分页查询信息：{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//缓存套餐信息 key: setmealCache:id
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐信息：{}", setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售禁售状态修改
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "套餐起售禁售状态修改")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@PathVariable Integer status,@RequestParam Long id){
        log.info("套餐起售禁售状态修改：{} {}", status, id);
        setmealService.updateStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id获取套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取套餐信息")
    //@Cacheable(value = "setmealCache",key = "#id")//缓存套餐信息 key: setmealCache:id
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("根据id获取套餐信息：{}",id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改套餐信息")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//删除套餐信息, key: setmealCache:id
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修啊套餐信息：{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "根据id批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)//删除套餐信息, key: setmealCache:id
    public Result delete(@RequestParam List<Long> ids){
        log.info("根据id批量删除套餐：{}",ids);
        setmealService.delete(ids);
        return Result.success();
    }
}
